<template>
  <div class="app-container">
    <el-input placeholder="输入关键字进行过滤" v-model="filterText"> </el-input>
    <el-tree
      :data="list"
      :props="defaultProps"
      :filter-node-method="filterNode"
      ref="tree"
    ></el-tree>
  </div>
</template>
<script>
import SubjectAPi from "@/api/subject";
export default {
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    },
  },
  data() {
    return {
      list: [],
      defaultProps: {
        children: "children",
        label: "title",
      },
      filterText: "",
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      SubjectAPi.subjectList().then((res) => {
        this.list = res.data.item;
      });
    },
    filterNode(value, data) {
      if (!value) {
        return true;
      }
      return data.title.indexOf(value) !== -1;
    },
  },
};
</script>